package com.jrx.anytxn.param.controller.account;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.account.SubjectAccountConfigReq;
import com.jrx.anytxn.param.dto.account.SubjectAccountConfigRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.account.ISubjectAccountConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 与页面相关的科目内部户配置controller
 *
 * @author yf
 * date 2019/12/24 0024
 * @version V1.0
 **/
@Api(tags = "科目内部户配置api")
@RestController
@RequestMapping("/param")
public class SubjectAccountConfigController {

    private static final Logger logger = LoggerFactory.getLogger(SubjectAccountConfigController.class);

    @Autowired
    private ISubjectAccountConfigService subjectAccountConfigService;

    @Autowired
    private InfoService infoService;

    @ApiOperation("根据租户ID分页查询科目内部户配置信息")
    @GetMapping("/subjectAccountConfig/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<SubjectAccountConfigRes>> getAllSubjectAccountConfigByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<SubjectAccountConfigRes>> result = new TxnRespResult<>();
        TxnPage<SubjectAccountConfigRes> res;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            res = subjectAccountConfigService.getAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("通过科目表ID和租户Id查询信息")
    @GetMapping("/subjectAccountConfig/subjectId/{subjectId}/tenantId/{tenantId}/productId/{productId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subjectId", value = "科目号", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "productId", value = "产品ID", required = true)})
    public TxnRespResult<SubjectAccountConfigRes> getBySubjectIdAndOrgId(@PathVariable(value = "subjectId") String subjectId, @PathVariable("tenantId") String tenantId,@PathVariable("productId") String productId) {
        TxnRespResult<SubjectAccountConfigRes> result = new TxnRespResult<>();
        SubjectAccountConfigRes res;
        try {
            res = subjectAccountConfigService.getByTenIdIdAndSubjectId(subjectId, String.valueOf(infoService.getUser().getProjectId()),productId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("增加科目内部户配置信息")
    @PostMapping("/subjectAccountConfig")
    public TxnRespResult<SubjectAccountConfigRes> createSubjectAccountConfig(@RequestBody SubjectAccountConfigReq subjectAccountConfigReq) {
        TxnRespResult<SubjectAccountConfigRes> result = new TxnRespResult<>();
        SubjectAccountConfigRes res;
        try {
            subjectAccountConfigReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            subjectAccountConfigReq.setUpdateBy(infoService.getUser().getUserName());
            res = subjectAccountConfigService.addSubjectAccountConfig(subjectAccountConfigReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        }
        return result;
    }

    @ApiOperation("修改科目内部户配置信息")
    @PutMapping("/subjectAccountConfig")
    public TxnRespResult<SubjectAccountConfigRes> modifySubjectAccountConfig(@RequestBody SubjectAccountConfigReq subjectAccountConfigReq) {
        TxnRespResult<SubjectAccountConfigRes> result = new TxnRespResult<>();
        SubjectAccountConfigRes res;
        subjectAccountConfigReq.setUpdateBy(infoService.getUser().getUserName());
        res = subjectAccountConfigService.modifySubjectAccountConfig(subjectAccountConfigReq);
        result.getSuccess(res);
        return result;
    }

    @ApiOperation("根据租户ID和科目表ID删除科目内部户配置信息")
    @DeleteMapping("/subjectAccountConfig/{tenantId}/{subjectId}/{productId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "subjectId", value = "科目表ID", required = true),
            @ApiImplicitParam(name = "productId", value = "产品ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndProIdAndSubId(@PathVariable(value = "tenantId") String tenantId, @PathVariable("subjectId") String subjectId,@PathVariable("productId") String productId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {

            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);
            flag = subjectAccountConfigService.removeBySubIdAndProIdAndTenId(String.valueOf(infoService.getUser().getProjectId()), subjectId,productId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getErrMsg(),e);
            result.getFail(e.getErrCode(), e.getErrMsg());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }


}
